package com.ihighest.app.infox.activity;

import java.util.LinkedList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ihighest.app.common.api.Api;
import com.ihighest.app.common.api.ApiCallback;
import com.ihighest.app.common.api.ApiParam;
import com.ihighest.app.common.api.ApiResult;
import com.ihighest.app.common.kit.ToastKit;
import com.ihighest.app.infox.R;
import com.ihighest.app.infox.adapter.MembersRecommendedAdapter;
import com.ihighest.app.infox.myview.ActivityTitle;
import com.ihighest.app.infox.myview.Tag;

/**
 * 发起群聊
 * 
 * @author lkx Created at 2015-10-14 上午9:41:30
 * @version 1.0
 */
public class LaunchGroupChatActivity extends BaseActivity {
	private ActivityTitle title;
	private ListView personListview;
	private MembersRecommendedAdapter adapter;

	private TextView btn_load_recommed_member, tv_tags;

	private EditText et_group_name;

	String tagIds;
	String tagTitles;
	private RelativeLayout chooseGroupLable;
	public List<String> memberids = new LinkedList<>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.infox_activity_lunchgroupchat);
		initView();
	}

	/**
	 * 初始化绑定
	 * 
	 * 
	 * @author lkx Created at 2015-10-14 下午3:34:53
	 */
	private void initView() {
		chooseGroupLable = (RelativeLayout) findViewById(R.id.chooseGroupLable);
		title = (ActivityTitle) findViewById(R.id.launchGroupChatActivityTitle);
		title.setTitle("发起群聊");
		title.setViewVersible(title.titleBack, title.rightText);
		title.setViewGone(title.titleMsg, title.titleRefresh);
		title.setRightTextViewText("确定");
		personListview = (ListView) findViewById(R.id.membersRecommendedListview);

		btn_load_recommed_member = (TextView) findViewById(R.id.btn_load_recommed_member);
		tv_tags = (TextView) findViewById(R.id.tv_tags);
		et_group_name = (EditText) findViewById(R.id.et_group_name);
		// 确定提交
		title.rightText.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				StringBuilder sb = new StringBuilder();
				for (String id : memberids) {
					sb.append(id).append(",");
				}
				Api.post(
						"chatgroup/create",
						ApiParam.create().addParam("title", et_group_name.getText().toString()).addParam("tags", tagIds)
								.addParam("members", sb.toString()), new ApiCallback(LaunchGroupChatActivity.this) {

							@Override
							public void handleSuccess(ApiResult result) {
								if (result.getCode() == 100) {
									ToastKit.showLong(LaunchGroupChatActivity.this, "申请创建成功，请等待审核");
									finish();
								} else {
									if (TextUtils.isEmpty(result.getMsg())) {
										ToastKit.showLong(LaunchGroupChatActivity.this, "申请创建失败");
									} else {
										ToastKit.showLong(LaunchGroupChatActivity.this, result.getMsg());
									}
								}
							}

							@Override
							public void handleFailure(Exception e, String msg) {
							}
						}, LaunchGroupChatActivity.this);
			}
		});
		// 选择标签
		chooseGroupLable.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				startActivityForResult(new Intent(LaunchGroupChatActivity.this, SelectLableActivity.class), 1);
			}
		});
		// 换一批
		btn_load_recommed_member.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				load();
			}
		});
		load();
	}

	/**
	 * 换一批
	 * 
	 * 
	 * @author Vity Created at 2015-10-21 下午2:53:37
	 */
	void load() {
		Api.get("chatgroup/recommend", ApiParam.Empty, new ApiCallback(this, true) {

			@Override
			public void handleSuccess(ApiResult result) {
				memberids.clear();
				adapter = new MembersRecommendedAdapter(LaunchGroupChatActivity.this, result.getDatas());
				personListview.setAdapter(adapter);
				adapter.notifyDataSetChanged();
			}

			@Override
			public void handleFailure(Exception e, String msg) {
			}
		}, this);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == RESULT_OK) {
			@SuppressWarnings("unchecked")
			List<Tag> result = (List<Tag>) data.getSerializableExtra("tags");
			if (result != null && !result.isEmpty()) {
				tagIds = "";
				tagTitles = "";
				for (Tag tag : result) {
					tagIds += tag.getId() + ",";
					tagTitles += tag.getTitle() + ",";
				}
				tagIds = tagIds.substring(0, tagIds.length() - 1);
				tagTitles = tagTitles.substring(0, tagTitles.length() - 1);
				tv_tags.setText(tagTitles);
			}
		}
	}
}
